package com.wx.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wx.entity.SmbmsProvider;
import com.wx.entity.SmbmsUser;
import com.wx.service.SmbmsProviderService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpSession;

/**
 * @author wangxiao
 * @since 2023-03-17 16:50:41
 */

@Controller
public class SmbmsProviderController {
    @Autowired
    HttpSession httpSession;

    @Autowired
    private SmbmsProviderService smbmsProviderService;

    @RequestMapping("/jsp/providerlist.do")
    public String providerlist(String pageIndex,String queryProCode , String queryProName) {
        int  currentPageNo = pageIndex==null?1:Integer.valueOf(pageIndex);
        Page page = Page.of(currentPageNo,5);
        SmbmsProvider smbmsProvider = new SmbmsProvider();
        smbmsProvider.setProCode(queryProCode);
        smbmsProvider.setProName(queryProName);
        IPage<SmbmsProvider> smbmsProviderIPage =  smbmsProviderService.getSmbmsProviderByPage(page,smbmsProvider);
        queryProCode = queryProCode == null ? "" : queryProCode;
        queryProName = queryProName == null ? "" : queryProName;
        httpSession.setAttribute("queryProCode",queryProCode);
        httpSession.setAttribute("queryProName",queryProName);
        httpSession.setAttribute("providerList",smbmsProviderIPage.getRecords());
        httpSession.setAttribute("totalPageCount",smbmsProviderIPage.getPages());
        httpSession.setAttribute("totalCount",smbmsProviderIPage.getTotal());
        httpSession.setAttribute("currentPageNo",currentPageNo);
        return "providerlist";
    }

    @RequestMapping("/jsp/addprovider.do")
    public String addprovider(SmbmsProvider smbmsProvider){
        //对象  ===>HashMap
        smbmsProvider.setCreationDate(new Date());
        smbmsProviderService.addSmbmsProvider(smbmsProvider);
        return   "redirect:/jsp/providerlist.do";
    }


    @RequestMapping("/jsp/delprovider.do")
    @ResponseBody
    public HashMap<String, String> delprovider(String proid) {
        //403 权限
        //404 页面没找到
        //400 参数问题
        //500  java
        System.out.println(proid);
        //调用service 实现真正的删除功能。
        smbmsProviderService.delSmbmsProvider(proid);
        HashMap<String, String> resultMap = new HashMap<>();
        resultMap.put("delResult", "true");
        return resultMap;
    }


    @RequestMapping("/jsp/viewprovider.do")
    public String viewprovider(String method,String proid){
        //对象  ===>HashMap
        System.out.println(method);
        System.out.println(proid);
        SmbmsProvider smbmsProvider =   smbmsProviderService.getSmbmsProviderById(proid);
        httpSession.setAttribute("provider",smbmsProvider);
        if(method.equals("view")){
            return   "providerview";
        }else {
            return   "providermodify";
        }
    }
    @RequestMapping("jsp/updprovider.do")
    public String updprovider(SmbmsProvider smbmsProvider){
        smbmsProviderService.updSmbmsProvider(smbmsProvider);
        return   "redirect:/jsp/providerlist.do";
    }


}